package com.company.project.controller;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.*;
import com.company.project.model.request.QueryParams;
import com.company.project.model.response.ShopDetail;
import com.company.project.service.FoodService;
import com.company.project.service.ShopInfoService;
import com.company.project.service.ShopLoginService;
import com.company.project.service.ShopService;
import com.company.project.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created by CodeGenerator on 2020/04/21.
*/
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private ShopService shopService;
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private FoodService foodService;
    @Autowired
    private ShopLoginService shopLoginService;

    /**
     * 商家注册
     * @param shop
     */
    @PostMapping("/register")
    public Result shopRegister(@RequestBody ShopLogin shop) {
        if(shop == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return shopService.shopRegister(shop);
    }

    /**
     * 检查商家邮箱是否重复
     */
    @GetMapping("/checkEmail")
    public Result checkEmail(String email) {
        if(StringUtils.isEmpty(email)) {
            return ResultGenerator.genFailResult("");
        }
        ShopLogin shopLogin = shopLoginService.findBy("email", email);
        if(shopLogin != null) {
            return ResultGenerator.genFailResult("该邮箱已被注册！");
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 添加商家详细信息
     * @param shop
     * @return
     */
    @PostMapping("/editShopDetail")
    public Result editShopDetail(@RequestBody ShopInfo shop) {
        if(shop == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        if(shop.getShopId() == null || shop.getShopId() == 0) {
            return ResultGenerator.genFailResult("邮箱信息错误");
        }
        return shopService.editShopDetail(shop);
    }

    /**
     * 根据商店类别获取商店列表
     * @param kind
     * @param page
     * @param size
     * @param shop
     * @return
     */
    @GetMapping("/get/shoplist/{kind}/{page}/{size}")
    public Result getShopList(@PathVariable("kind") String kind,
                              @PathVariable("page") Integer page,
                              @PathVariable("size") Integer size,
                              Shop shop) {
        PageHelper.startPage(page, size);
        List<Shop> shops = null;
        if(shop.getPrice() == null) {
            shops  = shopService.findList(kind, null, null);
        } else {
            shops = shopService.findList(kind, shop.getPrice(), shop.getPrice2());
        }
        PageInfo pageInfo = new PageInfo(shops);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * 获取商店详细信息
     * @param shopName
     * @return
     */
    @GetMapping("/detail/{shopName}")
    public Result getShopDetail(@PathVariable("shopName") String shopName) {
        ShopInfo shopInfo = shopInfoService.findBy("shopName", shopName);
        Condition condition = new Condition(Food.class);
        condition.createCriteria().andEqualTo("shopId", shopInfo.getShopId());

        List<Food> foodList = foodService.findByCondition(condition);
        List<String> imgList = new ArrayList<>();
        imgList.add(shopInfo.getLogoImg());
        imgList.add(shopInfo.getStoreImg());
        imgList.add(shopInfo.getInstoreImg());

        ShopDetail shopDetail = new ShopDetail();
        shopDetail.setFoodList(foodList);
        shopDetail.setShopInfo(shopInfo);
        shopDetail.setImgList(imgList);
        return  ResultGenerator.genSuccessResult(shopDetail);
    }

    /**
     * 获取热门搜索
     * @return
     */
    @GetMapping("/hotSearch")
    public Result getHotSearch() {
        return shopService.getHotSearch();
    }
    /**
     * 获取正在搜索的商家
     * @return
     */
    @GetMapping("/searching")
    public Result getSearching(String search) {
        if(StringUtils.isEmpty(search)) {
            return ResultGenerator.genSuccessResult();
        }
        return shopService.getSearching(search);
    }

    /**
     * 增加搜索次数
     * @param id
     * @return
     */
    @PostMapping("/addSearchNum")
    public Result addSearchNum(@RequestBody String id) {
        if(StringUtils.isEmpty(id)) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return shopService.addSearchNum(id);
    }

    @PostMapping("/admin/list")
    public Result getShopList(@RequestBody QueryParams queryParams){
        if(queryParams == null) {
            return  ResultGenerator.genFailResult("");
        }
        return shopService.getShopList(queryParams);
    }

    @PostMapping("/export")
    public Result export(@RequestBody List<ShopLogin> shopList) {
        return shopService.export(shopList);
    }


    @PostMapping("/deleteAll")
    public Result deleteAll(@RequestBody List<Shop> shopList) {
        if(shopList == null || shopList.size() <= 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return shopService.deleteAll(shopList);
    }

    @PostMapping("/update")
    public Result update(@RequestBody ShopLogin shop) {
        ShopLogin entity = shopLoginService.findBy("email", shop.getEmail());
        entity.setName(shop.getName());
        entity.setPhone(shop.getPhone());
        Shop s = shopService.findBy("shopId", entity.getShopId());
        ShopInfo shopInfo = shopInfoService.findBy("shopId", entity.getShopId());
        shopInfo.setAddress(shop.getAddress());
        s.setShopKind(shop.getShopKind());
        s.setAddress(shop.getAddress());
        shopService.update(s);
        shopLoginService.update(entity);
        shopInfoService.update(shopInfo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/add")
    public Result add(@RequestBody ShopLogin shop) {
        ShopLogin query = shopLoginService.findBy("email", shop.getEmail());
        if(query != null) {
            return ResultGenerator.genFailResult("邮箱已被注册!");
        }
        shop.setStatus("1");
        shop.setCreateTime(new Date());
        shop.setPassword(MD5Util.md5("123456", shop.getEmail()));
        shopLoginService.save(shop);
        ShopLogin entity = shopLoginService.findBy("email", shop.getEmail());

        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setShopId(entity.getShopId());
        shopInfo.setShopName(entity.getName());
        shopInfo.setShopKind(entity.getShopKind());
        shopInfo.setContactMobile(entity.getPhone());
        shopInfo.setBoxCost(new BigDecimal(0));
        shopInfo.setSendCost(new BigDecimal(0));
        shopInfo.setScore(new BigDecimal(0));
        shopInfo.setAddress(entity.getAddress());
        shopInfoService.save(shopInfo);

        Shop shop1 = new Shop();
        shop1.setShopId(entity.getShopId());
        shop1.setShopKind(shop.getShopKind());
        shop1.setShopName(entity.getName());
        shop1.setAddress(shop.getAddress());
        shopService.save(shop1);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer shopId) {
        if(shopId == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        Shop shop = shopService.findShopById(shopId);
        return ResultGenerator.genSuccessResult(shop);
    }

    @GetMapping("/info/{id}")
    public Result shopInfo(@PathVariable("id") Integer id) {
        if(id == null || id == 0) {
            return ResultGenerator.genFailResult("");
        }
        ShopInfo shop = shopInfoService.findBy("shopId", id);
        return ResultGenerator.genSuccessResult(shop);
    }

    @PostMapping("/editBase")
    public Result editBase(@RequestBody ShopInfo shopInfo) {
        if(shopInfo == null || shopInfo.getShopId() == null || shopInfo.getShopId() == 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return shopInfoService.editBase(shopInfo);
    }

    @PostMapping("/editImg")
    public Result editImg(@RequestBody ShopInfo shopInfo) {
        if(shopInfo == null || shopInfo.getShopId() == null || shopInfo.getShopId() == 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return shopInfoService.editImg(shopInfo);
    }

}
